package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.mapper.JdOrdersMapper;
import com.robotserver.orders.param.JdOrderApiPullDto;
import com.robotserver.orders.param.JdOrderSyncParam;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.param.biz.JdOrderPullParam;
import com.robotserver.orders.param.biz.TbOrderPullParam;
import com.robotserver.orders.service.JdOrderManualService;
import com.robotserver.orders.service.JdOrderService;
import com.robotserver.orders.service.TbOrderManualService;
import com.robotserver.orders.service.TbOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: jianghan
 * @Date: 2019/11/14 09:42
 * @Description:
 */
@RestController
public class JdOrderController {

    private static Logger logger = LoggerFactory.getLogger(JdOrderController.class);

    @Autowired
    private JdOrderService jdOrderService;
    @Autowired
    private JdOrderManualService jdOrderManualService;
    @Autowired
    private JdOrdersMapper jdOrdersMapper;

    /**
     * 本地下载
     * @param param
     * @return
     */
    @PostMapping("jdorder/pull")
    public Object pullOrder(JdOrderPullParam param){
        return jdOrderService.getOrderList(param);
    }

    /**
     * 远程api下载
     * @param jdOrderSyncParam
     * @return
     */
    @PostMapping("jdorder/remotePull")
    public Object remotePull(JdOrderSyncParam jdOrderSyncParam){
        if (jdOrderSyncParam.getUnionId()==null || StringUtils.isBlank(jdOrderSyncParam.getAuthKey())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        jdOrderManualService.remotePull(jdOrderSyncParam);
        return true;
    }

    /**
     * 远程下载所有
     * @param jdOrderSyncParam
     * @return
     */
    @PostMapping("jdorder/remote15Pull")
    public Object remote15Pull(JdOrderSyncParam jdOrderSyncParam){
        jdOrderService.syncOrder(jdOrderSyncParam);
        return true;
    }

    /**
     * 根据订单号获订单
     * @param orderId
     * @return
     */
    @PostMapping("jdorder/getByOrderId")
    public Object getByOrderId(String orderId){
        return jdOrderService.getByOrderId(orderId);
    }

    /**
     * 根据订单号商品id获订单
     * @return
     */
    @PostMapping("jdorder/getByordAndSku")
    public Object getByordAndSku(Long order_id,Long sku_id,Long createdt,String unionId,String authKey){
        return jdOrderService.getByordAndSku(order_id,sku_id,createdt,unionId,authKey);
    }

    /**
     * 远程api下载-V3
     * @param jdOrderSyncParam
     * @return
     */
    @PostMapping("jdorder/remotePullV3")
    public Object remotePullV3(JdOrderSyncParam jdOrderSyncParam){

        logger.info("--3.0手动远程下载京东订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(jdOrderSyncParam));

        //判断是否订单号为空，如果不为空，则先修改数据库此订单的最新修改时间
        if(StringUtils.isNotBlank(jdOrderSyncParam.getChildOrderNo())){
            jdOrdersMapper.updateUpdateTime(jdOrderSyncParam.getChildOrderNo());
        }

        if (jdOrderSyncParam.getBegin_time() == null
                || jdOrderSyncParam.getEnd_time() == null
                || jdOrderSyncParam.getDownTimeType() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        jdOrderService.syncOrder_byAppKey(jdOrderSyncParam);
        return true;
    }

    /**
     * 远程补拉全部联盟订单-3.0V
     * @param jdOrderSyncParam
     * @return
     */
    @PostMapping("jdorder/pullOrderByAllToken")
    public Object pullOrderByAllToken(JdOrderSyncParam jdOrderSyncParam){

        logger.info("--god平台手动远程下载京东订单--jdOrderSyncParam={}", JSONObject.toJSONString(jdOrderSyncParam));

        if (jdOrderSyncParam.getBegin_time() == null
                || jdOrderSyncParam.getEnd_time() == null
                || jdOrderSyncParam.getDownTimeType() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        if(jdOrderSyncParam.getDownTimeType() == null){
            jdOrderSyncParam.setDownTimeType(1); //下单时间
        }

        //modify on 2021-12-21 修改成将补拉订单放入redis当中
        jdOrderService.syncOrder_byAppKey_producer(jdOrderSyncParam);

        return true;
    }

    /**
     * 从API中拉取订单直接返回
     * @param jdOrderApiPullDto
     * @return
     */
    @PostMapping("/jdorder/api/pull")
    public Object apiPull(@RequestBody JdOrderApiPullDto jdOrderApiPullDto){
        return jdOrderService.apiPull(jdOrderApiPullDto);
    }
}
